﻿using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;
using Eassets.Layered.Domain.Secure;
using Eassets.Data.Global;


namespace Eassets.Data.Secure
{
    public class SecurityGroupData
    {
        /// <summary>
        /// Adds the specified security group.
        /// </summary>
        /// <param name="securityGroup">The security group.</param>
        public void Add(SecurityGroup securityGroup)
        {
            SqlCommand command = new SqlCommand("SP_AddSecurityGroup"); // Name of the SP
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, securityGroup));
        }

        /// <summary>
        /// Modifies the specified security group.
        /// </summary>
        /// <param name="securityGroup">The security group.</param>
        public void Modify(SecurityGroup securityGroup)
        {
            SqlCommand command = new SqlCommand("SP_ModifySecurityGroup"); // Name of the SP
            command.Parameters.AddWithValue("@SecurityGroupId", securityGroup.SecurityGroupId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, securityGroup));
        }

        /// <summary>
        /// Deletes the specified security group id.
        /// </summary>
        /// <param name="securityGroupId">The security group id.</param>
        public void Delete(long securityGroupId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteSecurityGroup"); // Name of the SP
            command.Parameters.AddWithValue("@SecurityGroupId", securityGroupId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// Gets the security group by id.
        /// </summary>
        /// <param name="securityGroupId">The security group id.</param>
        /// <returns></returns>
        public SecurityGroup GetSecurityGroupById(long securityGroupId)
        {
            try
            {
                SqlCommand command = new SqlCommand("SP_GetSecurityGroupByID");
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@SecurityGroupId", securityGroupId);

                SqlDataReader dataReader;
                try
                {
                    CommonData commonData = new CommonData();
                    dataReader = commonData.ExecuteReader(command);
                }
                catch (Exception exception1)
                {

                    throw;
                }


                SecurityGroup securityGroup = new SecurityGroup();
                while (dataReader.Read())
                {
                    securityGroup.SecurityGroupId = Convert.ToInt64(dataReader["SecurityGroupId"]);
                    securityGroup.Code = Convert.ToString(dataReader["Code"]);
                    securityGroup.Description = Convert.ToString(dataReader["Description"]);
                }
                dataReader.Close();
                return securityGroup;
            }
            catch (Exception exception)
            {

                throw;
            }

        }

        ///// <summary>
        ///// Gets all security groups.
        ///// </summary>
        ///// <returns></returns>
        //public Collection<SecurityGroup> GetSecurityGroupsByOrganizationLevelListId(long OrganizationLevelListId)
        //{
        //    Collection<SecurityGroup> securityGroups = new Collection<SecurityGroup>();

        //    SqlCommand command = new SqlCommand("SP_GetSecurityGroupsByOrganizationStructureId");
        //    command.CommandType = CommandType.StoredProcedure;

        //    command.Parameters.AddWithValue("@OrganizationLevelListId", OrganizationLevelListId);
        //    CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);

        //    SecurityGroup securityGroup;
        //    OrganizationLevelList organizationLevelList;

        //    while (dataReader.Read())
        //    {
        //        securityGroup = new SecurityGroup();

        //        securityGroup.SecurityGroupId = Convert.ToInt32(dataReader["SecurityGroupId"]);
        //        securityGroup.Code = dataReader["Code"].ToString();
        //        securityGroup.Description = dataReader["Description"].ToString();

        //        organizationLevelList = new OrganizationLevelList();

        //        organizationLevelList.OrganizationLevelListId = Convert.ToInt32(dataReader["OrganizationLevelListId"]);
        //        securityGroup.OrganizationLevelList = organizationLevelList;

        //        securityGroups.Add(securityGroup);
        //    }
        //    dataReader.Close();
        //    return securityGroups;
        //}

        /// <summary>
        /// GetAll securityGroup.
        /// </summary>
        /// <returns></returns>
        public Collection<SecurityGroup> GetAllSecurityGroup()
        {
            Collection<SecurityGroup> securityGroups = new Collection<SecurityGroup>();
            SqlCommand command = new SqlCommand("SP_GetAllSecurityGroups");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            SecurityGroup securityGroup;
            while (dataReader.Read())
            {
                securityGroup = new SecurityGroup();
                if (!(String.IsNullOrEmpty(dataReader["SecurityGroupId"].ToString())))
                {
                    securityGroup.SecurityGroupId = Convert.ToInt64(dataReader["SecurityGroupId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    securityGroup.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    securityGroup.Description = Convert.ToString(dataReader["Description"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    securityGroup.OrganizationLevelList = new OrganizationLevelListData().GetByIdOrganizationLevelList(securityGroup.OrganizationLevelListId);
                }

                securityGroups.Add(securityGroup);
            }
            dataReader.Close();
            return securityGroups;
        }

        /// <summary>
        /// Constructs the command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="securityGroup">The security group.</param>
        /// <returns></returns>
        public SqlCommand ConstructCommand(SqlCommand command, SecurityGroup securityGroup)
        {
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.AddWithValue("@Code", securityGroup.Code);
            command.Parameters.AddWithValue("@Description", securityGroup.Description);
            command.Parameters.AddWithValue("@OrganizationLevelListId", securityGroup.OrganizationLevelList.OrganizationLevelListId);

            return command;
        }
    }
}
